from rest_framework.viewsets import GenericViewSet
from . import paginations
from  rest_framework import mixins
from rest_framework.filters import SearchFilter,OrderingFilter
from rest_framework.views import APIView
from . import models,serializers
from .filters import CourseFilterSet
#pip3 install django-filter
from django_filters.rest_framework import DjangoFilterBackend

class CategoryListViewSet(mixins.ListModelMixin,GenericViewSet):
    #倒序排放,orm查询
    queryset = models.Department.objects.filter(is_delelte=False,is_show=True).all()
    #序列化
    serializer_class=serializers.CategorySerializers

class CompleteViewSet(mixins.ListModelMixin,GenericViewSet,mixins.RetrieveModelMixin):
    #倒序排放,orm查询
    queryset = models.Share_Content.objects.filter(is_delelte=False,is_show=True).all()
    #序列化
    serializer_class=serializers.CompleteSerializers
    #数据分页
    pagination_class = paginations.Base_Page

    #?search=搜索关键字，会全文匹配name字段,以下搜索均不可使用外键字段
    filter_backends = [SearchFilter, OrderingFilter, DjangoFilterBackend]
    search_fields = ['name']
    # ?ordering = id
    # 按点击次数升序
    #       ?ordering=-id按主键降序
    #       ?ordering=id 按主键升序
    #       ?ordering=-price,id 按点击次数降序，点击次数相同时按主键升序
    ordering_fields = ['cumulative_clicks', 'id']

    # 配置分类类
    filter_class = CourseFilterSet

    # 连用?ordering=id&search=搜索关键字



